const http = require('../../utils/http.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgArr:[],
    imgArr1: [],
    wishText:'',
    wishPrice:''
  },

//发布心愿单
  release(){
    var bl_wish = this.data.bl_wish;
    var bl_content = this.data.wishText;
    var bl_tps=this.data.imgArr1;
    var bl_yusuan = this.data.wishPrice;
    var bl_address_id = wx.getStorageSync('nowAddress').id;
    console.log(bl_tps);
    http.fetchApi('procedure_t', { p: `R2013006|${bl_wish}|${bl_content}|${bl_tps}|${bl_yusuan}|${bl_address_id}` }).then(res=>{
      if(res.data.code==1){
        
        wx.showModal({
          content: '你的心愿单已发布成功，我们将在三日内抽取获奖的心愿单，希望有你哦，祝你心愿成真。',
          showCancel: false,
          confirmColor: '#02BB00',
          success:(res)=> {
            if (res.confirm) {
              this.setData({
                imgArr: [],
                imgArr1: [],
                wishText: '',
              })

              var pages = getCurrentPages();
              //上一个页面实例对象
              var prePage = pages[pages.length - 2];
              var day2 = new Date();
              day2.setTime(day2.getTime());
              var s2 = day2.getFullYear() + "-" + (day2.getMonth() + 1) + "-" + day2.getDate();
              prePage.querySign(s2);
              wx.navigateBack({
                
              })
            } 
          }
        })
      }else{
        http.showToast('提交失败!' + '', '', 'none')
      }
    })
   
  },

  //删除图片
  deleteImg(e){
    var imgArr=this.data.imgArr;
    var idx = e.currentTarget.dataset.idx;
    imgArr.splice(idx,1);
    this.setData({
      imgArr
    })
  },

  //上传图片
  chooseImg(){
    var imgArr=this.data.imgArr;
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success:(res)=> {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        imgArr.push(tempFilePaths)
        console.log(imgArr)
        this.setData({
          imgArr
        })

        wx.uploadFile({
          url: `${app.globalData.host}file`,
          filePath: tempFilePaths[0],
          name: 'file',
          success:  (ret)=> {
            var imgArr1=this.data.imgArr1;
           // if (ret.data.code==1){
          
            var data = JSON.parse(ret.data)
            console.log(data)
              imgArr1.push(data.url)
              this.setData({
                imgArr1
              })
           // }
            
          }
        })


      }
    })
  },

  releaseWish(e){
    var wishText = e.detail.value
   this.setData({
     wishText
   })
  },

  //获取心愿单预算
  getWishPrice(){
    http.fetchApi('procedure_t', { p:`R2015004|0`}).then(res=>{
      if(res.data.code==1){
        var wishPrice = res.data.results[0].jine;
        this.setData({
          wishPrice
        })
      }
    })
  },
  //选择收货地址
  chooseAddress() {
    wx.navigateTo({
      url: '../goodsAddress/goodsAddress?page=takeExpress'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bl_wish: options.wishId
    })
    this.getWishPrice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //取出默认地址放到地址栏
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: 'R2003008|' + uid }).then(res => {
      if (res.data.code == 1) {
        var addressArr = res.data.results;
        var defaultAddress = addressArr.filter(item => item.isdefault);

        if (!wx.getStorageSync('nowAddress')) {
          wx.setStorageSync('nowAddress', defaultAddress[0]);
          var nowAddress = wx.getStorageSync('nowAddress');

          this.setData({
            nowAddress
          })
        } else {
          var nowAddress = wx.getStorageSync('nowAddress');

          this.setData({
            nowAddress
          })
        }


      } else {
        http.showToast('服务器好像出错啦!' + '', '', 'none')
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})